package src.main.java.org.collection;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.function.Consumer;

//Collection的遍历方式
public class Test3 {
    public static void main(String[] args) {
        Collection<String> c = new ArrayList<>();
        c.add("小王");
        c.add("小张");
        c.add("小刘");
        c.add("小何");

        Iterator<String> iterator = c.iterator();
        System.out.println(iterator.next());
        System.out.println(iterator.next());
        System.out.println(iterator.next());
        System.out.println(iterator.next());
        //使用循环结合迭代器遍历
        while (iterator.hasNext()){
            System.out.println(iterator.next());
        }
        System.out.println("==============");

        //增强for循环
        for (String name:c) {
            System.out.println(name);
        }
        System.out.println("==============");
        //lambda遍历
       /* c.forEach(new Consumer<String>() {
            @Override
            public void accept(String s) {
                System.out.println(s);
            }
        });*/
        //c.forEach(s -> System.out.println(s));
        c.forEach(System.out::println);
    }
}
